home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / TPIFile1.0b1 / TPIFileTest / TPIFileTest.c next >
Encoding:
C/C++ Source or Header  |  1997-04-09  |  7.5 KB  |  301 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TPIFileTest.c
  3.  
  4.     Contains:    A trivial test program for the TPIFile module.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. /////////////////////////////////////////////////////////////////////
  22. // Pick up all the OT client info, specifically the OTRegisterPort.
  23.  
  24. #include <OpenTransport.h>
  25.  
  26. /////////////////////////////////////////////////////////////////////
  27. // OK, so it's yet another console based sample from Quinn!
  28.  
  29. #include <stdio.h>
  30. #include <limits.h>
  31.  
  32. /////////////////////////////////////////////////////////////////////
  33. // Pick up the name of the TPIFile port.
  34.  
  35. #include "TPIFile.h"
  36.  
  37. /////////////////////////////////////////////////////////////////////
  38.  
  39. // Define a global buffer to hold the data we're reading out of the file.
  40. // 
  41. // Note the number of bytes that we read out of the endpoint in a single
  42. // hit is not the same chunk size as used by the TPIFile module to read
  43. // the file (which is 2KB).  This is a deliberate test of the way OT
  44. // endpoint libraries do deblocking.
  45.  
  46. enum {
  47.     kBufferSize = 1000
  48. };
  49.  
  50. static char gBuffer[kBufferSize];
  51.  
  52. /////////////////////////////////////////////////////////////////////
  53.  
  54. static OSStatus TestTPIFile(ConstFSSpecPtr fss, UInt32 byteCount, Boolean disconnect)
  55.     // Test the TPIFile module by opening an endpoint to the
  56.     // file specified by fss and printing the contents to
  57.     // the console window.
  58. {
  59.     OSStatus err;
  60.     EndpointRef ep;
  61.     TCall sndCall;
  62.     FileSpecAddress connectAddr;
  63.     OTFlags junkFlags;
  64.     long i;
  65.     
  66.     // Open the endpoint to our module.
  67.     
  68.     ep = OTOpenEndpoint(OTCreateConfiguration(kTPIFilePortName), 0, nil, &err);
  69.     
  70.     // Switch to sync/blocking mode to simplify our code and then
  71.     // do a null bind in preparation for the connect.
  72.     if (err == noErr) {
  73.         (void) OTSetBlocking(ep);
  74.         (void) OTSetSynchronous(ep);
  75.         err = OTBind(ep, nil, nil);
  76.     }
  77.     
  78.     // Connect to a file specified by an AF_FILESPEC address format.
  79.     if (err == noErr) {
  80.         connectAddr.fAddressType = AF_FILESPEC;
  81.         connectAddr.fss = *fss;
  82.         sndCall.addr.buf = (UInt8 *) &connectAddr;
  83.         sndCall.addr.len = sizeof(connectAddr);
  84.         sndCall.opt.buf = nil;
  85.         sndCall.opt.len = 0;
  86.         sndCall.udata.buf = nil;
  87.         sndCall.udata.len = 0;
  88.         sndCall.sequence = 0;
  89.         err = OTConnect(ep, &sndCall, nil);
  90.     }
  91.     
  92.     // A standard OTRcv and print loop.
  93.     if (err == noErr) {
  94.         do {
  95.             err = OTRcv(ep, gBuffer, kBufferSize, &junkFlags);
  96.             if (err > noErr) {
  97.                 for (i = 0; i < err; i++) {
  98.                     if (byteCount == 0) {
  99.                         err = noErr;
  100.                         goto premature_close;
  101.                     }
  102.                     byteCount -= 1;
  103.                     putchar(gBuffer[i]);
  104.                 }
  105.                 fflush(stdout);
  106.                 err = noErr;
  107.             }
  108.         } while (err == noErr);
  109.     }
  110.  
  111. premature_close:
  112.     if (err == noErr && disconnect) {
  113.         err = OTSndDisconnect(ep, nil);
  114.     }
  115.  
  116.     // When we can't read any more data, consult and print the reason.    
  117.     if (err == kOTLookErr) {
  118.         printf("Got kOTLookErr.\n");
  119.         err = OTLook(ep);
  120.         switch (err) {
  121.             case T_DISCONNECT:
  122.                 printf("T_DISCONNECT\n");
  123.                 break;
  124.             default:
  125.                 printf("Unknown look %d\n", err);
  126.         }
  127.         err = kOTLookErr;
  128.     }
  129.     
  130.     if (ep != kOTInvalidEndpointRef) {
  131.         (void) OTCloseProvider(ep);
  132.     }
  133.     return (err);
  134. }
  135.  
  136. /////////////////////////////////////////////////////////////////////
  137.  
  138. static OSStatus TestBogusConnect(void)
  139.     // Test how well the TPI module handles being asked to connect
  140.     // to a bogus FSSpec.
  141. {
  142.     OSStatus err;
  143.     FSSpec bogusFileSpec;
  144.     
  145.     (void) FSMakeFSSpec(0, 0, "\pBogus Victim", &bogusFileSpec);
  146.     
  147.     err = TestTPIFile(&bogusFileSpec, 1024, false);
  148.     
  149.     return (err);
  150. }
  151.  
  152. /////////////////////////////////////////////////////////////////////
  153.  
  154. static OSStatus TestConnectAsync(ConstFSSpecPtr fss, Boolean disconnect)
  155.     // Test an asynchronous connect operation followed immediately
  156.     // by a disconnect.
  157. {
  158.     OSStatus err;
  159.     EndpointRef ep;
  160.     TCall sndCall;
  161.     FileSpecAddress connectAddr;
  162.         
  163.     ep = OTOpenEndpoint(OTCreateConfiguration(kTPIFilePortName), 0, nil, &err);
  164.     
  165.     // Switch to sync/blocking mode to simplify our code and then
  166.     // do a null bind in preparation for the connect.
  167.     if (err == noErr) {
  168.         (void) OTSetBlocking(ep);
  169.         (void) OTSetSynchronous(ep);
  170.         err = OTBind(ep, nil, nil);
  171.     }
  172.     
  173.     // Connect to a file specified by an AF_FILESPEC address format.
  174.     if (err == noErr) {
  175.         (void) OTSetAsynchronous(ep);
  176.         connectAddr.fAddressType = AF_FILESPEC;
  177.         connectAddr.fss = *fss;
  178.         sndCall.addr.buf = (UInt8 *) &connectAddr;
  179.         sndCall.addr.len = sizeof(connectAddr);
  180.         sndCall.opt.buf = nil;
  181.         sndCall.opt.len = 0;
  182.         sndCall.udata.buf = nil;
  183.         sndCall.udata.len = 0;
  184.         sndCall.sequence = 0;
  185.         err = OTConnect(ep, &sndCall, nil);
  186.         if (err == kOTNoDataErr) {
  187.             err = noErr;
  188.             printf("Connecting\n");
  189.         }
  190.     }
  191.     if ( (err == noErr) && disconnect) {
  192.         err = OTSndDisconnect(ep, nil);
  193.         if (err == noErr) {
  194.             printf("Waiting for disconnect");
  195.             while ( OTGetEndpointState(ep) != T_IDLE ) {
  196.                 if ( TickCount() % 10 == 0 ) {
  197.                     printf(".");
  198.                 }
  199.             }
  200.             printf("\n");
  201.         }
  202.     }
  203.  
  204.     if (ep != kOTInvalidEndpointRef) {
  205.         (void) OTCloseProvider(ep);
  206.     }
  207.     
  208.     return (err);
  209. }
  210.  
  211. /////////////////////////////////////////////////////////////////////
  212.  
  213. static OSStatus TestConnectAsyncWithExtraEndpoint(ConstFSSpecPtr fss, Boolean disconnect)
  214. {
  215.     OSStatus err;
  216.     EndpointRef ep;
  217.     char junkStr[256];
  218.     
  219.     ep = OTOpenEndpoint(OTCreateConfiguration(kTPIFilePortName), 0, nil, &err);
  220.     if (err == noErr) {
  221.         err = TestConnectAsync(fss, disconnect);
  222.     }
  223.     if (err == noErr) {
  224.         printf("Press return to continue.\n");
  225.         gets(junkStr);
  226.     }
  227.     
  228.     return (err);
  229. }
  230.  
  231.  
  232. /////////////////////////////////////////////////////////////////////
  233.  
  234. void main(void)
  235. {
  236.     OSStatus err;
  237.     FSSpec fileToTest;
  238.     char commandStr[256];
  239.     
  240.     printf("Hello Cruel World!\n");
  241.     fflush(stdout);
  242.     
  243.     err = InitOpenTransport();
  244.     
  245.     if (err == noErr) {
  246.     
  247.         // Hardwire this code to read a file called "Victim" in the
  248.         // same folder as the application.
  249.         
  250.         err = FSMakeFSSpec(0, 0, "\pVictim", &fileToTest);
  251.  
  252.         if (err == noErr) {
  253.             printf("a) Read and print all.\n");
  254.             printf("b) Read and print with premature close.\n");
  255.             printf("c) Read and print with premature disconnect.\n");
  256.             printf("d) Connect to a bogus file.\n");
  257.             printf("e) Connect async then fast close.\n");
  258.             printf("f) Connect async then fast disconnect.\n");
  259.             printf("g) Connect async then fast close with extra endpoint.\n");
  260.             printf("Enter the test you want to perform:\n");
  261.             gets(commandStr);
  262.             
  263.             switch (commandStr[0]) {
  264.                 case 'a':
  265.                     err = TestTPIFile(&fileToTest, ULONG_MAX, false);
  266.                     break;
  267.                 case 'b':
  268.                     err = TestTPIFile(&fileToTest, 1024, false);
  269.                     break;
  270.                 case 'c':
  271.                     err = TestTPIFile(&fileToTest, 1024, true);
  272.                     break;
  273.                 case 'd':
  274.                     err = TestBogusConnect();
  275.                     break;
  276.                 case 'e':
  277.                     err = TestConnectAsync(&fileToTest, false);
  278.                     break;
  279.                 case 'f':
  280.                     err = TestConnectAsync(&fileToTest, true);
  281.                     break;
  282.                 case 'g':
  283.                     err = TestConnectAsyncWithExtraEndpoint(&fileToTest, false);
  284.                     break;
  285.                 default:
  286.                     printf("Huh?\n");
  287.                     err = -1;
  288.                     break;
  289.             }
  290.         }
  291.         
  292.         CloseOpenTransport();
  293.     }
  294.     
  295.     if (err == noErr) {
  296.         printf("Success.\n");
  297.     } else {
  298.         printf("Failed with error %d.\n", err);
  299.     }
  300.     printf("Done.  Press command-Q to Quit.\n");
  301. }